/**

 @author 石头哥哥
 </P>
 Date:   2014/12/17
 </P>
 Time:   21:06

 注解：

 */
package GameObject {
import Qbase.utils.sound.SoundManager;

import flash.geom.Point;

import lzm.starling.STLConstant;

import starling.animation.IAnimatable;
import starling.animation.Tween;

import starling.core.Starling;
import starling.display.DisplayObject;

import starling.display.Image;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.events.Event;
import starling.textures.Texture;

import utils.Kit;

public class Enemy extends Sprite implements IAction {

    /**
     *  当前血量
     */
    private var _hp:int;

    public function set hp(value:int):void {
        _hp = value;
    }

    public function get hp():int {
        return _hp;
    }

    /**
     *  子弹类型
     */
    private var bulletType:int;
    /**
     * 敌机初始化位置
     */
    private var x_postion:int = 0;
    private var y_postion:int = 0;

    /**
     * 子弹移动速度
     */
    private var bulletSpeed:int = 0;

    /**
     * 子弹贴图类型
     */
    private var type:int = 0;


    /**
     * 可造成的伤害值  碰撞
     */
    private var _hurt:int;

    public function get hurt():int {
        return _hurt;
    }


    private var obj:Sprite;


    private var _score:int = 0;


    public function get score():int {
        return _score;
    }

    /**
     *
     * @param type
     */
    public static function HurtByType(type:int):int {
        if (type == 1)return 2;
        if (type == 2)return 5;
        if (type == 3)return 10;
        return -1;
    }


    /**
     * 创建
     * @param type     1,2,3
     * @param hp       1~10
     * @param bulletType   1
     * @param x_postion
     * @param y_postion
     * @param parent
     * @param bulletSpeed   10~20
     */
    public function Enemy(obj:Sprite, type:int, x_postion:int, y_postion:int, bulletType:int = 1, bulletSpeed:int = 10) {
        super();
        this.type = type;
        this.bulletType = bulletType;
        this.x_postion = x_postion;
        this.y_postion = y_postion;
        this.bulletSpeed = bulletSpeed;
        this._hurt = HurtByType(this.type);
        this.obj = obj;
    }


    /**
     * 根据贴图类型 初始化  对象
     */
    private var moveTween:Tween;

    public function init():void {
        var enemy:Texture;
        if (type == 1) {
            enemy = GameSystem.assetManager.getTexture("enemy1");
            _hp = 10;
            _score = 10;
        } else if (type == 2) {
            enemy = GameSystem.assetManager.getTexture("enemy2");
            _hp = 20;
            _score = 20;
        } else if (type == 3) {
            enemy = GameSystem.assetManager.getTexture("enemy3_n1");
            _hp = 50;
            _score = 50;
        }
        if (enemy) {
            var image:Image = new Image(enemy);
            this.addChild(image);
            addMoved();
        } else {
            throw  Error("create bullet texture failed " + type);
        }
    }

    //add enemy move tween
    private function addMoved():void {
        var mind:Number = STLConstant.StageWidth - this.width;
        this.x = Kit.Rnd(0, mind);
        this.y = 0;
        moveTween = new Tween(this, (STLConstant.StageHeight + 100) / 80);
        moveTween.moveTo(this.x, STLConstant.StageHeight + 10);//MOVED 1290
        Starling.juggler.add(moveTween);
        moveTween.onComplete = function ():void {
            Starling.juggler.removeTweens(moveTween);
        };

        if (type == 3) {
            SoundManager.getInstance().playSound("sound/big_spaceship_flying.mp3");
        }
    }


    /**
     * 创建子弹对象
     * @return
     */
    public function fireX():Bullet {


        return null;

    }


    private var fireAnimatable:IAnimatable;

    /**
     *  fire
     */
    public function fire():void {
        fireAnimatable = Starling.juggler.repeatCall(fire0, Kit.Rnd(0.5, 1.0));  //调用子弹 时间间隔 随机 1，2
    }

    /**
     *
     */
    private function fire0():void {
        if (!fireAnimatable) {
            throw Error("");
        }

        if (type == 3) {
            var offset:int = 0;
            for (var i:uint = 0; i != 3; ++i) {

                var bullet:Bullet = new Bullet(this.bulletType,
                        bulletSpeed, hurt);
                if (bullet) {
                    bullet.init();
                    bullet.x = this.x + offset;
                    bullet.y = this.y + this.height;
                    offset += this.width / 2;

                    bullet.touchable = false;
                    obj.addChild(bullet);// add  enemy
                    //manager of bullet
                    GameManger.enemyBulletList.push(bullet);
                    Starling.juggler.add(bullet);//begin fire
                }
            }
        } else if (type == 1) {

            var bullet:Bullet = new Bullet(this.bulletType,
                    bulletSpeed, hurt);
            if (bullet) {
                bullet.init();
                bullet.x = this.x + this.width / 2;
                bullet.y = this.y + this.height;

                bullet.touchable = false;
                obj.addChild(bullet);// add  enemy
                //manager of bullet
                GameManger.enemyBulletList.push(bullet);
                Starling.juggler.add(bullet);//begin fire
            }
        } else if (type == 2) {
            var offset:int = 0;
            for (var i:uint = 0; i != 2; ++i) {
                var bullet:Bullet = new Bullet(this.bulletType,
                        bulletSpeed, hurt);
                if (bullet) {
                    bullet.init();
                    bullet.x = this.x + offset;
                    bullet.y = this.y + this.height;
                    offset += this.width;


                    obj.addChild(bullet);// add  enemy
                    bullet.touchable = false;
                    //manager of bullet
                    GameManger.enemyBulletList.push(bullet);
                    Starling.juggler.add(bullet);//begin fire

                }
            }

        }
        SoundManager.getInstance().playSound("sound/bullet.mp3");
    }


    /**
     *
     * @return
     */
    public function downAnimateByType():MovieClip {
        if (type == 1) {
            SoundManager.getInstance().playSound("sound/enemy1_down.mp3");
            return new MovieClip(GameSystem.assetManager.getTextures("enemy1_down"));
        }
        if (type == 2) {
            SoundManager.getInstance().playSound("sound/enemy2_down.mp3");
            return new MovieClip(GameSystem.assetManager.getTextures("enemy2_down"));
        }
        if (type == 3) {
            SoundManager.getInstance().playSound("sound/enemy3_down.mp3");
            return new MovieClip(GameSystem.assetManager.getTextures("enemy3_down"));
        }
        throw Error("enemy type is error:" + type);
    }


    /**
     * 停止游戏
     */
    public function pause():void {
        // fire
        if (fireAnimatable)Starling.juggler.remove(fireAnimatable);
        //move  enemy
        if (moveTween)Starling.juggler.remove(moveTween);
    }

    /**
     * 恢复游戏状态
     */
    public function reuse():void {
        // fire
        if (fireAnimatable)Starling.juggler.add(fireAnimatable);
        //move
        if (moveTween)Starling.juggler.add(moveTween);

    }


    /**
     * clear obj of this
     * this.y max limit is STLConstant.StageHeight+10
     */
    public function destroy(dispose:Boolean = false):Boolean {
        if (_hp <= 0 || this.y >= STLConstant.StageHeight) {
            this.removeFromParent(dispose);
            if (fireAnimatable) {
                Starling.juggler.remove(fireAnimatable);
                fireAnimatable = null;
            }
            return true;
        }
        return false;
    }


    /**
     * clear obj of this
     * this.y max limit is STLConstant.StageHeight+10
     */
    public function clear(dispose:Boolean = false):void {
        this.removeFromParent(dispose);
        if (fireAnimatable) {
            Starling.juggler.remove(fireAnimatable);
            fireAnimatable = null;
        }
        //move
        if (moveTween)Starling.juggler.remove(moveTween);
        this.dispose();
    }

    /**
     * 采用圆心碰撞 检查方式
     * @param target
     * @return
     */
    public function collision(target:DisplayObject):Boolean {
        var raid:int = Math.max(this.width, this.height) / 2 + Math.max(target.width, target.height) / 2;//半径之和
        if (Point.distance(new Point(this.x + this.width / 2, this.y + this.height / 2),
                new Point(target.x + target.width / 2, target.y + target.height / 2)) <= raid) {
            if (target is Hero) {
                var hero:Hero = target as Hero;
                _hp -= 1;
                hero.hp -= 1;
            }
            return true;
        }
        return false;
    }
}
}
